
#include "LinkList.h"

/*³õÊ¼»¯¿Õ±í*/

Status Init_Linklist(polynomial *L)
{
  *L=(polynomial)malloc(sizeof(polyNodeh));

  (*L)->link =NULL;

 return OK;
}
 polynomial creat_LinkList(){
   polynomial p=(polynomial)malloc(sizeof(polyNodeh));
   return p;
 }
 void link_LinkList(polynomial p1,polynomial p2){
   p1->link=p2;
   p1=p2;
 }

   void print_LinkList(polynomial p)
   {
    polynomial ll=p;
      descOrder(ll);
    polynomial t=p->link;
    if(!t){
        printf("0 0");
    } else {
        while(t){

            if(t->coef != 0){

                if(t->expon==0) printf("%d",t->coef);

                else if(t->coef == 1 && t->expon == 1) printf("x");

                else if(t->coef == -1 && t->expon == 1) printf("-x");

                else if(t->coef == 1 && t->expon != 1) printf("x^%d",t->expon);

                else if(t->coef == -1 && t->expon != 1) printf("-x^%d",t->expon);

                else if((t->coef != 1 || t->coef != -1) && t->expon == 1)  printf("%dx",t->coef);

                else if((t->coef != 1 || t->coef != -1) && t->expon != 1)  printf("%dx^%d",t->coef,t->expon);

                if(t->link && t->link->coef>0) printf("+");
                t=t->link;
            }
        }
    }



      /* polynomial ll=head;
      descOrder(ll);
       while(head->link->link!=NULL){
            if(head->link->coef!=1){
             printf("%d",head->link->coef);
            }
            if(head->link->expon!=0){
              if(head->link->expon==1){
                printf("x");
            }else{
              printf("x^%d+",head->link->expon);
            }
           }


        head=head->link;
       }
        if(head->link->coef!=1){
               printf("%d",head->link->coef);
            }
          if(head->link->expon!=0){
              if(head->link->expon==1){
                printf("x");
            }else{
              printf("x^%d",head->link->expon);
            }
           }*/


   }
//求两个数相加；
  polynomial add_LinkList(polynomial head1,polynomial head2)
{
    polynomial init1=NULL,l;
    init1 =(polynomial)malloc(sizeof(polyNodeh));
    init1->link=NULL;
    l=init1;
     polynomial ll0=head1;
      descOrder(ll0);
      polynomial ll2=head2;
      descOrder(ll2);
    while( head1->link!=NULL&&head2->link!=NULL){
        if(head1->link->expon==head2->link->expon){
          polynomial sum=creat_LinkList();
          sum->coef=head1->link->coef+head2->link->coef;
          sum->expon=head1->link->expon;
          l->link=sum;
          l=sum;
          l->link=NULL;
          head1=head1->link;
          head2=head2->link;

        }
      else if(head1->link->expon>head2->link->expon)
        {
            polynomial sum= creat_LinkList();
            sum->coef=head1->link->coef;
            sum->expon=head1->link->expon;
             l->link=sum;
             l=sum;
             l->link=NULL;
            head1=head1->link;


        }
     else  if(head1->link->expon<head2->link->expon)
        {
           polynomial sum= creat_LinkList();
            sum->coef=head2->link->coef;
            sum->expon=head2->link->expon;
            l->link=sum;
            l=sum;
            l->link=NULL;
            head2=head2->link;

        }

    }
        if(head1->link==NULL){

            while(head2->link!=NULL){
              polynomial temporary=creat_LinkList();
              temporary->coef=head2->link->coef;
              temporary->expon=head2->link->expon;
              l->link=temporary;
              l=temporary;
              l->link=NULL;
              head2=head2->link;

            }
        }
        else if(head2->link==NULL){
            while(head1->link!=NULL){
              polynomial temporary=creat_LinkList();
              temporary->coef=head1->link->coef;
              temporary->expon=head1->link->expon;
              l->link=temporary;
              l=temporary;
              l->link=NULL;
              head1=head1->link;

            }
        }
  return init1;
  }

  //多项式相减
  polynomial subtraction_LinkList(polynomial head1,polynomial head2){
   polynomial ll0=head1;
      descOrder(ll0);
      polynomial ll2=head2;
      descOrder(ll2);
   polynomial init1=NULL,l;
    init1 =(polynomial)malloc(sizeof(polyNodeh));
    init1->link=NULL;
    l=init1;

    while( head1->link!=NULL&&head2->link!=NULL){
        if(head1->link->expon==head2->link->expon){
          polynomial sum=creat_LinkList();
          sum->coef=head1->link->coef-head2->link->coef;
          sum->expon=head1->link->expon;
          l->link=sum;
          l=sum;
          l->link=NULL;
          head1=head1->link;
          head2=head2->link;

        }
      else if(head1->link->expon>head2->link->expon)
        {
            polynomial sum= creat_LinkList();
            sum->coef=head1->link->coef;
            sum->expon=head1->link->expon;
             l->link=sum;
             l=sum;
             l->link=NULL;
            head1=head1->link;


        }
     else  if(head1->link->expon<head2->link->expon)
        {
           polynomial sum= creat_LinkList();
            sum->coef=-head2->link->coef;
            sum->expon=head2->link->expon;
            l->link=sum;
            l=sum;
            l->link=NULL;
            head2=head2->link;

        }

    }
        if(head1->link==NULL){

            while(head2->link!=NULL){
              polynomial temporary=creat_LinkList();
              temporary->coef=-head2->link->coef;
              temporary->expon=head2->link->expon;
              l->link=temporary;
              l=temporary;
              l->link=NULL;
              head2=head2->link;

            }
        }
        else if(head2->link==NULL){
            while(head1->link!=NULL){
              printf("head11111111\n");
              polynomial temporary=creat_LinkList();
              temporary->coef=head1->link->coef;
              temporary->expon=head1->link->expon;
              l->link=temporary;
              l=temporary;
              l->link=NULL;
              head1=head1->link;

            }
        }
  return init1;
  }
  //多项式想乘
  polynomial multiply_LinkList(polynomial head1,polynomial head2){
        polynomial ll0=head1;
      descOrder(ll0);
      polynomial ll2=head2;
      descOrder(ll2);
      int sum=0 ,a=0,b=0,c=0;
  polynomial init1=NULL,l,mulitHead1,init2,finalInit,finalInitTest;
   mulitHead1=head1;
    init1 =(polynomial)malloc(sizeof(polyNodeh));
    finalInit=(polynomial)malloc(sizeof(polyNodeh));
    finalInit->link=NULL;
    finalInitTest=finalInit;
    init1->link=NULL;
    l=init1;
    init2=init1;
     while(head2->link!=NULL){
        while(head1->link!=NULL){
            polynomial temporary1= creat_LinkList();
            temporary1->coef=head1->link->coef*head2->link->coef;
            temporary1->expon=head1->link->expon+head2->link->expon;
            l->link=temporary1;
            l=temporary1;
            l->link=NULL;
            head1=head1->link;
        }
        head2=head2->link;
        head1=mulitHead1;
     }
   while(init2->link!=NULL){
        polynomial tinital2=init2;
        polynomial tinital3;
          tinital2=tinital2->link;
        while(tinital2->link!=NULL){

         if(init2->link->expon==tinital2->link->expon){
            init2->link->coef=init2->link->coef+tinital2->link->coef;
            tinital3=tinital2->link;
            init2->link->link=tinital2->link->link;
                 tinital2=tinital3;
         }else{
         tinital2=tinital2->link;

         }

        }
        init2=init2->link;

     }
    return init1;
   }
void descOrder(polynomial L)
{
    int data,index;
    polynomial p1 = L->link;
    polynomial p2 = L->link;
    while(p1!=NULL)
    {
        p2 = L->link;
        while(p2!=NULL)
        {
            if(p1->expon > p2->expon)
            {
                data = p1->coef;
                index = p1->expon;
                p1->coef = p2->coef;
                p1->expon = p2->expon;
                p2->coef = data;
                p2->expon = index;
            }
            p2 = p2->link;
        }
        p1 = p1->link;
    }
}
